home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / AIAT / Headers / Index / TermIndex.h < prev    next >
Encoding:
Text File  |  1998-04-16  |  10.4 KB  |  330 lines  |  [TEXT/CWIE]

  1. // TermIndex.h
  2. //    Copyright:    © 1994 - 1998 by Apple Computer, Inc., all rights reserved.
  3.  
  4. //// TermIndex: a base class for both vector and inverted indices.
  5. //// Maintains a dictionary of terms.
  6.  
  7. #pragma once
  8. #ifndef TermIndex_h
  9. #define TermIndex_h
  10.  
  11. #pragma import on
  12.  
  13. #if PRAGMA_STRUCT_ALIGN
  14.     #pragma options align=power
  15. #endif
  16.  
  17. #include "IAIndex.h"
  18.  
  19. #include <time.h>
  20.  
  21. #pragma IA_BEGIN_EXPORTS
  22.  
  23. typedef uint32        TermID;
  24. typedef uint32        TermFreq;
  25. //typedef uint32    TermOffset;
  26.  
  27. typedef TermID        DocID;
  28. typedef TermFreq    DocLength;
  29.  
  30.  
  31. const uint32        TermIndexType = 'Ter2';
  32.  
  33. //// TermInfo: statistics about a term's occurrence.
  34.  
  35. // Subclasses augment, in particular to store postings, forming an inverted index.
  36. class TermInfo : public IAOrderedStorable {
  37. public:
  38.                 TermInfo();                        // term = NULL;
  39.                 TermInfo(IATerm* t, TermID i) : term(t), fID(i), docCount(0) {}
  40.     IA_INLINE    ~TermInfo() IA_INLINE_DEF_BODY(delete term)
  41.  
  42.  
  43.     // IAOrderedStorable methods
  44.     IAStorable*    DeepCopy() const;
  45.     IABlockSize    StoreSize() const;
  46.     void        Store(IAOutputBlock* output) const;
  47.     IAStorable*    Restore(IAInputBlock* input) const;
  48.     bool        LessThan(const IAOrderedStorable* other) const;    // return term->LessThanNonVirtual(other->term);
  49.     bool        Equal(const IAOrderedStorable* other) const;    // return term->EqualNonVirtual(other->term);
  50.     IATerm*        GetTerm() const {return term;}
  51.     void        SetTerm(IATerm*     t) {term = t;}
  52.     TermID        GetTermID() const {return fID;}
  53.     void        SetDocumentCount(TermFreq dCount) {docCount = dCount;}
  54.     TermFreq    GetDocumentCount() const {return docCount;}
  55.     
  56. protected:
  57.     void        DeepCopying(const IAStorable* source);
  58.     void        Restoring(IAInputBlock* input, const IAStorable* proto);
  59. private:
  60.                 TermInfo(TermInfo&);                // don't define a copy constructor
  61.                 
  62.     IATerm*     term;                            // the term
  63.     TermID         fID;                                // its ID
  64.     TermFreq    docCount;                        // the number of docs it occurs in
  65.  
  66. };
  67.  
  68. //// DocInfo: statistics about a document.
  69.  
  70. // Subclasses augment, in particular to store vectors.
  71. class DocInfo : public IAOrderedStorable {
  72. public:
  73.                 DocInfo() : doc(NULL) {}
  74.                 DocInfo(IADoc* d, DocID i) : doc(d), fID(i), length(0) {}
  75.     IA_INLINE    ~DocInfo() IA_INLINE_DEF_BODY(delete doc)
  76.  
  77.  
  78.     // IAOrderedStorable methods
  79.     IAStorable*    DeepCopy() const;
  80.     IABlockSize    StoreSize() const;
  81.     void        Store(IAOutputBlock* output) const;
  82.     IAStorable*    Restore(IAInputBlock* input) const;
  83.     bool        LessThan(const IAOrderedStorable* other) const;    // return doc->LessThan(other->doc);
  84.     bool        Equal(const IAOrderedStorable* other) const;    // return doc->Equal(other->doc);
  85.     IADoc*        GetDocument() const {return doc;}
  86.     DocID        GetDocID() const {return fID;}
  87.     void        SetDocumentLength(DocLength dlength) {length = dlength;}
  88.     DocLength    GetDocumentLength() const {return length;}
  89.     void        SetDocument(IADoc*        d) {doc = d;}
  90. protected:
  91.     void        DeepCopying(const IAStorable* source);
  92.     void        Restoring(IAInputBlock* input, const IAStorable* proto);
  93. private:
  94.                 DocInfo(DocInfo&);            // don't define a copy constructor
  95.                 
  96.     IADoc*        doc;                        // the document
  97.     DocID         fID;                            // its ID
  98.     DocLength    length;                        // the number of terms in the doc
  99.                 
  100. };
  101.  
  102. // internal types needed for private member declarations
  103. struct FreqPosting;
  104. class TFVector;
  105. class TFMap;
  106. class Progress;
  107. class TermUpdateSet;
  108. class DocUpdateSet;
  109.  
  110. // flush progress report support
  111. typedef void FlushProgressFn(float percent, void* data);
  112.  
  113. //// TermIndex: base class for vector & inverted indices
  114.  
  115. class TermIndex : public IAIndex {
  116.     friend class IAQuery;                // for GetTFMaps()
  117. public:
  118.             TermIndex(IAStorage* s, IACorpus* c, IAAnalysis* a,
  119.                       uint32 t = TermIndexType, IABlockID r = NULL);
  120.             ~TermIndex();
  121.     
  122.     // IAIndex methods we'll modify
  123.     void                Initialize();
  124.     void                Open();
  125.  
  126.     void                Flush();
  127.  
  128.     void                AddDoc(IADoc* doc);
  129.     void                AddDoc(IADoc* doc, DocID* returnID);
  130.     void                RenameDoc(const IADoc* oldName, const IADoc* newName);
  131.     void                DeleteDoc(const IADoc* doc);
  132.  
  133.     bool                IsDocIndexed(const IADoc* doc);
  134.  
  135.     IADocIterator*        GetDocIterator();
  136.     IADocIterator*        GetDocIterator(const IADoc* start);
  137.  
  138.     void                Merge(IAIndex** indices, uint32 indexCount);
  139.  
  140.     // TermIndex-specific methods
  141.     
  142.     virtual void    Purge();
  143.     // TermInfo's & ID's
  144.     virtual TermInfo*            GetTermInfo(IATerm* term);
  145.  
  146.     IATerm*                GetIDTerm(TermID termid);
  147.     TermID                GetMaxTermID();
  148.     TermFreq            GetTermCount();
  149.  
  150.     IAOrderedStorableIterator*    GetTermInfoIterator();
  151.     IAOrderedStorableIterator*    GetTermInfoIterator(IATerm* start);
  152.  
  153.     // DocInfo's & ID's
  154.     DocInfo*            GetDocInfo(const IADoc* doc, bool noError = false);
  155.  
  156.     IADoc*                GetIDDoc(DocID docid);
  157.     DocID                GetMaxDocID();
  158.     DocID                GetDocCount();
  159.  
  160.     IAOrderedStorableIterator*    GetDocInfoIterator();
  161.     IAOrderedStorableIterator*    GetDocInfoIterator(const IADoc* start);
  162.     
  163.     // Called under AddDoc(), DeleteDoc() and Flush().
  164.     void SetFlushProgressFn(FlushProgressFn* fn) {flushProgressFn = fn;}
  165.     FlushProgressFn*    GetFlushProgressFn() const {return flushProgressFn;}
  166.     void SetFlushProgressData(void* pdata) {flushProgressData = pdata;}
  167.     void*    GetFlushProgressData() const {return flushProgressData;}
  168.     void SetFlushProgressFreq(clock_t freq) {flushProgressFreq = freq;}
  169.     clock_t    GetFlushProgressFreq() const {return flushProgressFreq;}
  170.     
  171.     
  172.  
  173.  
  174.     IADefineNarrowMethods(TermIndex, IAIndex);    // support for IANarrow
  175.  
  176.     bool Validate(bool verbose = true, bool justDocs = true);        // validate this inddex
  177.     virtual bool ValidateTermInfos(bool verbose);
  178.     virtual bool ValidateDocInfos(bool verbose);
  179.     
  180.     virtual uint32        GetNorm();
  181. protected:
  182.     void                Initializing();
  183.     // methods which add some data to the index root block
  184.     IABlockSize            RootSize();
  185.     void                StoreRoot(IAOutputBlock* output);
  186.     void                RestoreRoot(IAInputBlock* input);
  187.     // To be augmented by subclasses:
  188.     //  . for inverted indices:
  189.     virtual TermInfo*    MakeTermInfo(IATerm* term, TermID termid);
  190.     virtual TermInfo*    UpdateTermInfo(TermInfo* i, FreqPosting* adds, TermFreq addCount, TermFreq delCount);
  191.     virtual TermInfo*    MergeTermInfo(TermInfo* i, TermInfo* addTi, TermIndex* addIndex, DocID base);
  192.     virtual void        MergeDocIDs(TermIndex* addIndex, DocID base);
  193.     virtual uint32        MergeTermCost();        // used for merge progress estimations
  194.     virtual uint32        MergeDocCost();            // used for merge progress estimations
  195.     //  . vector indices
  196.     virtual DocInfo*    MakeDocInfo(IADoc* doc, DocID docID);
  197.     virtual void        UpdateDocInfo(DocInfo* i, TFVector* vector);
  198.     virtual void        MergeDocInfo(DocInfo* i, DocInfo* addDi, TermIndex* addIndex, TermID* reMap);
  199.  
  200.     // update support
  201.     virtual void        FinishingUpdate();
  202.     virtual void        DeletingDoc(DocInfo* docInfo);
  203.     virtual void        DeletingPostings(IATerm* term, TermFreq delCount);
  204.     virtual void        FlushUpdates();
  205.     // delete doc clean up
  206.     virtual void        ResetPostings(TermInfo* terminfo);
  207.  
  208.     // Used by RankedQuery.  Default impl here retokenizes, optimized by VectorIndex.
  209.     virtual bool        GetTFMaps(IADoc** docs, uint32 nDocs, TFMap** tfMaps, Progress* p);
  210.     
  211.     // default constructor etc. so that this can be a virtual base class
  212.             TermIndex();
  213.     void    Constructing(IAStorage* s, IACorpus* c, IAAnalysis* a, uint32 t, IABlockID r);
  214.     IAOrderedStorableSet*    GetTermInfoSet() const {return termInfoSet;}
  215.     IAOrderedStorableSet*    GetiDTermMap() const {return iDTermMap;}
  216.     void SetUpdateCount(uint32 cnt) {updateCount = cnt;}
  217.     uint32 GetUpdateCount() const {return updateCount;}
  218.     
  219.     void                SetBytesForUpdate(uint32 bupdate) {bytesForUpdate = bupdate;} // amount of memory available
  220.     uint32                GetBytesForUpdate() const {return bytesForUpdate;}    
  221.     
  222. private:
  223.     void                MaybeFlushUpdates();
  224.     void                FlushTermUpdates(Progress* prog);
  225.     void                FlushDocUpdates(Progress* prog);
  226.  
  227.     void                MergeTermInfos(IAIndex** indices, DocID* bases, Progress* prog);
  228.     void                MergeDocInfos(IAIndex** indices, DocID* bases, Progress* prog);
  229.  
  230.     // The substance of the index.
  231.     IAOrderedStorableSet*    docInfoSet;
  232.     IAOrderedStorableSet*    iDDocMap;
  233.     IAOrderedStorableSet*    termInfoSet;
  234.     IAOrderedStorableSet*    iDTermMap;
  235.     IAOrderedStorableSet**    mergeMaps;                // NULL except while merging
  236.  
  237.     uint32                updateCount;
  238.     
  239.     // queued updates.
  240.     TermUpdateSet*        termUpdateSet;
  241.     DocUpdateSet*        docUpdateSet;
  242.     
  243.     // persistent slots -- written in root
  244.     IABlockID            docInfoSetRoot;
  245.     IABlockID            iDDocMapRoot;
  246.     IABlockID            termInfoSetRoot;
  247.     IABlockID            iDTermMapRoot;
  248.     DocID                maxDocID;
  249.     TermID                maxTermID;
  250.     uint32                mergeMapCount;                // NULL except while merging
  251.     uint32*                mergeMapRoots;                // NULL except while merging
  252.  
  253.     FlushProgressFn*    flushProgressFn;
  254.     void*                flushProgressData;
  255.     clock_t                flushProgressFreq;
  256.     
  257.     uint32                bytesForUpdate;            // amount of memory available
  258.  
  259.  
  260.                         TermIndex(TermIndex&);        // don't define a copy constructor
  261. };
  262.  
  263.  
  264. IAExceptionCode                InvalidDocID = 'VIID';
  265. IAExceptionCode                InvalidTermID = 'VIIT';
  266.  
  267.  
  268. //// IDTerm: used in iDTermMap to map from TermIDs back to Terms -- ordered by decreasing id
  269. class IDTerm : public IAOrderedStorable {
  270. public:
  271.                 IDTerm();                    // term = NULL;
  272.                 IDTerm(TermID i, IATerm* t) : fID(i), term(t) {}
  273.                 ~IDTerm();                    // delete term;
  274.  
  275.     // IAOrderedStorable methods
  276.     IAStorable*    DeepCopy() const;
  277.     IABlockSize    StoreSize() const;
  278.     void        Store(IAOutputBlock* out) const;
  279.     IAStorable*    Restore(IAInputBlock* in) const;
  280.     bool        LessThan(const IAOrderedStorable* other) const;    // return fID > ((IDTerm*)other)->fID;
  281.     bool        Equal(const IAOrderedStorable* other) const;    // return fID == ((IDTerm*)other)->fID;
  282.     TermID        GetTermID() const {return fID;}                        
  283.     IATerm*        GetTerm() const {return term;}
  284.     void        SetTerm(IATerm* t) {term = t;}
  285.     void        SetTermID(TermID ID){ fID = ID;}
  286.  
  287. private:
  288.                 IDTerm(IDTerm&);            // don't define a copy constructor
  289.                 
  290.     TermID        fID;                        // the id
  291.     IATerm*        term;                        // and corresponding term
  292.  
  293.  
  294. };
  295.  
  296.  
  297. //// IDDoc: used in iDDocMap to map from DocIDs back to docs -- ordered by decreasing id
  298. class IDDoc : public IAOrderedStorable {
  299. public:
  300.                 IDDoc();                    // doc = NULL;
  301.                 IDDoc(DocID i, IADoc* d) : fID(i), doc(d) {}
  302.                 ~IDDoc();                    // delete doc;
  303.  
  304.  
  305.     // IAOrderedStorable methods
  306.     IAStorable*    DeepCopy() const;
  307.     IABlockSize    StoreSize() const;
  308.     void        Store(IAOutputBlock* out) const;
  309.     IAStorable*    Restore(IAInputBlock* in) const;
  310.     bool        LessThan(const IAOrderedStorable* other) const;    // return fID > ((IDDoc*)other)->fID;
  311.     bool        Equal(const IAOrderedStorable* other) const;    // return fID == ((IDDoc*)other)->fID;
  312.     DocID        GetDocID() const {return fID;}                        
  313.     IADoc*        GetDocument() const {return doc;}
  314.     void        SetDocument(IADoc*        d) {doc = d;}
  315. private:
  316.                 IDDoc(IDDoc&);            // don't define a copy constructor
  317.     DocID         fID;                            // the ID
  318.     IADoc*        doc;                        // the document
  319.  
  320. };
  321.  
  322. #pragma IA_END_EXPORTS
  323.  
  324. #if PRAGMA_STRUCT_ALIGN
  325.     #pragma options align=reset
  326. #endif
  327.  
  328. #pragma import reset
  329. #endif
  330.